import React from 'react';
import { Router, Route, Link, browserHistory } from 'react-router';

import Navigation from './common/Navigation';
import api from './api';
//路由
import router from './router';

import {message } from 'antd';

import 'layout.scss';
import 'antd.css';
import 'height.scss';
import 'margin.scss';
import 'flex.scss';
import 'ant_custom.scss';

message.config({
  top: 100,
  duration: 2,
});

export default class App extends React.Component{

    constructor(props, context){
        super(props, context);
        this.state={
           session:{}
        };
    }
    
    async componentWillMount(){
        let session = await this.checkLogin();
       
        if(!session.cd){
            this.setState({
                session:session.data
            })
        }
    }
    async checkLogin(){
        return await api('/authority/check/login');
    }
    async requireAuth(nextState,replaceState){
        var session = await this.checkLogin();
        if(session.cd==102){
            location.href='/login?next='+(nextState.location.pathname||"/") ;
            return {employee:{},userPassport:{}};
        }
        return session;
    }

    render(){
        return (
            <Navigation >
                <Router history={browserHistory}>
                {router.map((item,index)=>{
                        return (<Route session={this.requireAuth.bind(this)} onEnter={this.requireAuth.bind(this)} key={index} path={item.path} component={item.name}/>)
                    })
                }
                </Router>
            </Navigation>
        );
    }
}
